package  
{
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	/**
	 * ...
	 * @author David Rangel
	 */
	public class Main extends MovieClip
	{
		var MC_tela			:MovieClip = new MovieClip();
		var MC_tela2		:MovieClip = new MovieClip();
		var MC_nave			:Nave;
		var MC_tiro			:Tiro;
		var MC_tiro2		:Tiro2;
		var MC_inimigo		:Inimigo;
		var MC_fundo		:Fundo;
		var MC_vida			:Vida;
		var gameover		:GameOver;
		var playagain		:PlayAgain;
		
		
		
		var UI_vida			:uint = 1;
		
		var IN_delayTiro 	:int = 1;
		var IN_delayInimigo :int = 30;
		var IN_delayTiroInimigo :int = 30;
		
		var AR_tiro 		:Array;
		var AR_inimigo		:Array;
		var AR_nave			:Array;
		var AR_tiroInimigo  :Array;
		
		var BO_cima :Boolean = false;
		var BO_baixo :Boolean = false;
		var BO_esq :Boolean = false; 
		var BO_dir :Boolean = false;
		var BO_tiro:Boolean = false;
		
		var TX_score:TextField;
		var TX_scoreFormat:TextFormat;
		var UI_score:uint;
		
		var musica:Sound = new Musica();
		var channel:SoundChannel = new SoundChannel();
		
		
		public function Main() 
		{
			
			
			iniciarJogo();
			
		}
		
		private function iniciarJogo():void 
		{
			channel = musica.play();
			
			AR_inimigo = new Array();
			AR_tiro = new Array();
			AR_nave = new Array();
			AR_tiroInimigo = new Array();
			
			addEventListener(Event.ENTER_FRAME, update)
			stage.addEventListener(KeyboardEvent.KEY_DOWN, pressionar)
			stage.addEventListener(KeyboardEvent.KEY_UP, soltar)
			
			addChild(MC_tela);

			MC_fundo = new Fundo();
			MC_fundo.x = stage.stageWidth;
			MC_fundo.y = stage.stageHeight / 2;
			MC_tela.addChild(MC_fundo);
			
			MC_nave = new Nave();
			MC_nave.x = 20
			MC_nave.y = stage.stageHeight / 2;
			MC_tela.addChild(MC_nave);
			Score();
			
			MC_vida = new Vida();
			MC_vida.x = 20;
			MC_vida.y = 10;
			MC_tela.addChild(MC_vida);
			
		}
		
				
				
		private function update(e:Event):void 
		{
 	 		inimigo();
			moverNave();
			limiteTela();
			colidir();
			colisao_inimigo_nave();
			colisao_tiro_inimigo();
			colisaoTiroAsa();
			TX_score.text = "Score  " + UI_score.toString();
			trace (UI_score);
			
			tiroInimigo();
			
			
			MC_fundo.update();
			
				for each (MC_tiro in AR_tiro)
				{
					MC_tiro.update();
				}
				
				for each (MC_tiro2 in AR_tiroInimigo)
				{
					MC_tiro2.update();
				}
			
				for each (MC_inimigo in AR_inimigo)
				{
					MC_inimigo.update();
				}
				
				
				
		}
		
		private function Score():void
		{
			TX_score = new TextField();
			TX_scoreFormat = new TextFormat();
			
			
			TX_score.type = TextFieldType.DYNAMIC
			TX_score.width = 200;
			TX_score.height = 50;
			TX_score.x = 650;
			TX_score.y = 10;
			TX_score.textColor = 110000;
			
			TX_scoreFormat.size = 20;
			TX_scoreFormat.align = "justify";
			TX_scoreFormat.bold = true;
			TX_scoreFormat.font = "Quartz MS";
			TX_score.defaultTextFormat = TX_scoreFormat;
			addChild(TX_score)
			
			
			
		}
		
		
		private function pressionar(e:KeyboardEvent):void 
		{
			switch(e.keyCode)
			{
				case Keyboard.UP:
				BO_cima = true;
				break;
		
				case Keyboard.DOWN:
				BO_baixo = true;
				break;
			
				case Keyboard.LEFT:
				BO_esq = true;
				break;
		
				case Keyboard.RIGHT:
				BO_dir = true;
				break;
				
				case Keyboard.SPACE:
				BO_tiro = true;
				break;
				
			}
			
		}
		
		private function soltar(e:KeyboardEvent):void 
		{
			switch(e.keyCode)
			{
				case Keyboard.UP:
				BO_cima = false;
				break;
		
				case Keyboard.DOWN:
				BO_baixo = false;
				break;
			
				case Keyboard.LEFT:
				BO_esq = false;
				break;
		
				case Keyboard.RIGHT:
				BO_dir = false;
				break;
				
				case Keyboard.SPACE:
				BO_tiro = false;
				break;
			}
		}
		
		private function moverNave():void 
		{
			if(BO_dir == true)
			{
				MC_nave.x += 5
			}
	
			if(BO_esq == true)
			{
				MC_nave.x -= 5
			}
	
			if(BO_cima == true)
			{
				MC_nave.y -= 5
			}
	
			if(BO_baixo == true)
			{
				MC_nave.y += 5
			}
			
			if (BO_tiro == true)
			{
				atirar();
			}
			
		}
		
		private function atirar():void
		{
			IN_delayTiro--
				if (IN_delayTiro <= 0)
				{
					MC_tiro = new Tiro(1);
					MC_tiro.x = MC_nave.x + 40;
					MC_tiro.y = MC_nave.y;
					MC_tela.addChild(MC_tiro)
					AR_tiro.push(MC_tiro)
					
					IN_delayTiro = 5;
				}
		}
		
		public function inimigo():void
		{
			IN_delayInimigo--;
			if(IN_delayInimigo <= 0)			
				{
					IN_delayInimigo = 30;
					MC_inimigo = new Inimigo();
					MC_inimigo.x = stage.stageWidth - 50; 
					MC_inimigo.y = 300 * Math.random();
					MC_tela.addChild(MC_inimigo);
					AR_inimigo.push(MC_inimigo);
					IN_delayInimigo = 30;
				}
		}
		
		private function tiroInimigo():void
		{
			IN_delayTiroInimigo--;
			if (IN_delayTiroInimigo <= 0)
			{
				MC_tiro2 = new Tiro2(2);
				MC_tiro2.x = MC_inimigo.x - 30;
				MC_tiro2.y = MC_inimigo.y;
				MC_tela.addChild(MC_tiro2);
				AR_tiroInimigo.push(MC_tiro2);
				IN_delayTiroInimigo = 30;
				
			}
		}
		

		private function colidir ():void
		{
			for (var i:int = 0; i < AR_tiro.length; i++)
			{
				for (var j:int = 0; j < AR_inimigo.length; j++)
				{
					if (AR_tiro[i] != null && AR_inimigo[j] != null)
					{
						if (AR_tiro[i].hitTestObject(AR_inimigo[j]))
						{
							MC_tela.removeChild(AR_tiro[i]);
							MC_tela.removeChild(AR_inimigo[j]);
							AR_tiro.splice(i, 1);
							AR_inimigo.splice(j, 1);
							UI_score += 10;
						}
					}
				}
			}
		}
		
		private function colisao_inimigo_nave ():void
		{
			for (var a:int = 0; a < AR_inimigo.length; a++)
			{				
				if (AR_inimigo[a] != null)
				{
					if (AR_inimigo[a].hitTestObject(MC_nave.hitbox))
					{
						UI_vida++;
						MC_vida.gotoAndStop(UI_vida);
						MC_tela.removeChild(AR_inimigo[a])
						AR_inimigo.splice(a, 1);
						if (UI_vida == 4)
						{
							gameOver();
						}
						
					}
					if (AR_inimigo[a].hitTestObject(MC_nave.hitbox2))
					{
						UI_vida++;
						MC_vida.gotoAndStop(UI_vida);
						MC_tela.removeChild(AR_inimigo[a])
						AR_inimigo.splice(a, 1);
						if (UI_vida == 4)
						{
							gameOver();
						}
					}
				}
			}
		}
		private function colisao_tiro_inimigo():void
		{
			for (var c:int = 0; c < AR_tiroInimigo.length; c++)
			{
				if (AR_tiroInimigo[c] != null)
				{
					if (AR_tiroInimigo[c].hitTestObject(MC_nave.hitbox))
					{
						UI_vida++;
						MC_vida.gotoAndStop(UI_vida);
						MC_tela.removeChild(AR_tiroInimigo[c])
						AR_tiroInimigo.splice(c, 1);
						if (UI_vida == 4)
						{
							gameOver();
						}
					}
				}
			}
		}
		
		private function colisaoTiroAsa():void
		{
			for (var aa:int = 0; aa < AR_tiroInimigo.length; aa++) 
			{
				if (AR_tiroInimigo[aa] != null)
				{
					if (AR_tiroInimigo[aa].hitTestObject(MC_nave.hitbox2))
					{
						UI_vida++;
						MC_vida.gotoAndStop(UI_vida);
						MC_tela.removeChild(AR_tiroInimigo[aa])
						AR_tiroInimigo.splice(aa, 1);
						if (UI_vida == 4)
						{
							gameOver();
						}
					}
				}
				
			}	
		}
		
		private function gameOver():void
		{
			removeChild(MC_tela);
			channel.stop();
			
			
			AR_inimigo.length = 0;
			AR_nave.length = 0;
			AR_tiro.length = 0;
			AR_tiroInimigo.length = 0
			UI_vida = 1
			
			removeEventListener(KeyboardEvent.KEY_DOWN, pressionar)
			removeEventListener(KeyboardEvent.KEY_UP, soltar)
			removeEventListener(Event.ENTER_FRAME, update)
			
			addChild(MC_tela2);
			gameover = new GameOver();
			gameover.x = stage.stageWidth / 2;
			gameover.y = stage.stageHeight / 2;
			MC_tela2.addChild(gameover);
			
			playagain = new PlayAgain();
			playagain.x = stage.stageWidth / 2;
			playagain.y = 300;
			MC_tela2.addChild(playagain);
			
			playagain.addEventListener(MouseEvent.MOUSE_DOWN, clicar);
			
			Score();
		}
		
		private function clicar(e:MouseEvent):void
		{
			
			removeChild(MC_tela2);
			
			playagain.removeEventListener(MouseEvent.MOUSE_DOWN, clicar);
			UI_score = 0;
			iniciarJogo();
		}
		
		
		// Limite de Tela da Nave.
		
		
		private function limiteTela ():void
		{
			if (MC_nave.x <= 0 + MC_nave.width/2)
			{
				MC_nave.x = 0 + MC_nave.width /2;
			}
			
			if (MC_nave.x >= 800 - MC_nave.width / 2)
			{
				MC_nave.x = 800 - MC_nave.width / 2;
			}
			
			if (MC_nave.y <= 0 + MC_nave.height / 2)
			{
				MC_nave.y = 0 + MC_nave.height / 2;
			}
			
			if (MC_nave.y >= 400 - MC_nave.height / 2)
			{
				MC_nave.y = 400 - MC_nave.height / 2;
			}
		}
		
		
		
		
	}

	
	
}